package com.ideaaedi.springcloud.jd.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.google.common.collect.Sets;
import com.ideaaedi.springcloud.jd.commonds.entity.user.po.SysRoleMenuRePO;
import com.ideaaedi.springcloud.jd.commonds.entity.user.vo.req.SysRoleBindMenuReqVO;
import com.ideaaedi.springcloud.jd.commonds.entity.user.vo.req.SysRoleQueryMenuReqVO;
import com.ideaaedi.springcloud.jd.commonds.entity.user.vo.resp.SysQueryMenuRespVO;
import com.ideaaedi.springcloud.jd.commonspring.mybatisplus.ServiceImplExt;
import com.ideaaedi.springcloud.jd.user.mapper.SysRoleMenuReMapper;
import com.ideaaedi.springcloud.jd.user.service.SysMenuService;
import com.ideaaedi.springcloud.jd.user.service.SysRoleMenuReService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 系统-角色菜单中间表 服务实现类
 * </p>
 *
 * @author <font size = "20" color = "#3CAA3C"><a href="https://gitee.com/JustryDeng">JustryDeng</a></font> <img src="https://gitee.com/JustryDeng/shared-files/raw/master/JustryDeng/avatar.jpg" />
 * @since 2021.0.1.A
 */
@Service
public class SysRoleMenuReServiceImpl extends ServiceImplExt<SysRoleMenuReMapper, SysRoleMenuRePO> implements SysRoleMenuReService {
    
    @Resource
    SysMenuService sysMenuService;
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<SysQueryMenuRespVO> bindMenu(SysRoleBindMenuReqVO req) {
        Long roleId = req.getRoleId();
        Set<Long> menuIds = req.getMenusIds();
        // 删除原来的
        remove(new LambdaQueryWrapper<SysRoleMenuRePO>()
                .eq(SysRoleMenuRePO::getRoleId, roleId)
        );
        
        // 添加新的
        if (!CollectionUtils.isEmpty(menuIds)) {
            List<SysRoleMenuRePO> list = menuIds.stream().map(menuId -> {
                SysRoleMenuRePO re = new SysRoleMenuRePO();
                re.setId(IdWorker.getId());
                re.setMenuId(menuId);
                re.setRoleId(roleId);
                return re;
            }).collect(Collectors.toList());
            saveBatch(list);
        }
        SysRoleQueryMenuReqVO sysRoleQueryMenuReq = new SysRoleQueryMenuReqVO();
        sysRoleQueryMenuReq.setRoleIds(Sets.newHashSet(roleId));
        return queryMenu(sysRoleQueryMenuReq);
    }
    
    @Override
    public List<SysQueryMenuRespVO> queryMenu(SysRoleQueryMenuReqVO req) {
        Set<Long> menuIdSet = list(new LambdaQueryWrapper<SysRoleMenuRePO>()
                .in(SysRoleMenuRePO::getRoleId, req.getRoleIds())
        ).stream()
                .map(SysRoleMenuRePO::getMenuId)
                .collect(Collectors.toSet());
    
        List<SysQueryMenuRespVO> list = Collections.emptyList();
        if (!CollectionUtils.isEmpty(menuIdSet)) {
            list = sysMenuService.listByIds(menuIdSet).stream().map(x -> {
                SysQueryMenuRespVO resp = new SysQueryMenuRespVO();
                resp.setName(x.getName());
                resp.setId(x.getId());
                return resp;
            }).collect(Collectors.toList());
        }
        return list;
    }
    
}
